<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Promise 封装 AJAX</title>
    <link crossorigin='anonymous' href="https://cdn.bootcss.com/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</head>
<body>
    <div class="container">
        <h2 class="page-header">Promise 封装 AJAX 操作</h2>
        <button class="btn btn-primary" id="btn">点击发送 AJAX</button>
    </div>
    <script type="text/javascript">
        // 接口地址：https://api.apiopen.top/api/sentences
        $("#btn").click(function () {
            // 传统方法
            // $.ajax({
            //     url: "https://api.apiopen.top/api/sentences",
            //     data: "",
            //     type: "GET",
            //     success: function (data) {
            //         console.log(data);
            //     },
            //     dataType: "json"
            // });

            // Promise
            const p = new Promise((resolve, reject) => {
                $.ajax({
                    url: "https://api.apiopen.top/api/sentences",
                    data: "",
                    type: "GET",
                    success: function (data) {
                        resolve(data);
                    },
                    dataType: "json"
                });
            });
            
            p.then((val) => {
                console.log(val);
            }, (err) => {
                
            });

            return false;
        });
    </script>
</body>
</html>